
#include "mainwindow.h"
#include "dockermanager.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
  , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    manager = new DockerManager(this);

    auto images = manager->images();
    foreach (auto var, images) {
        if (var.repository() == "<none>") continue;
        QListWidgetItem *item = new QListWidgetItem;
        item->setText(var.repository() + ":" + var.tag());
        item->setData(990, var.id());
        ui->docker_images_list->addItem(item);
    }

    connect(ui->docker_ps_show_all, &QCheckBox::stateChanged, this, [this](){
        load_docker_ps();
    });

    load_docker_ps();

    ui->docker_images_data_splitter->setSizes(QList<int>()
                                              << ui->docker_images_data_splitter->height() * 0.8
                                              << ui->docker_images_data_splitter->height() * 0.2);
    ui->docker_ps_data_splitter->setSizes(QList<int>()
                                              << ui->docker_ps_data_splitter->height() * 0.8
                                              << ui->docker_ps_data_splitter->height() * 0.2);
}

MainWindow::~MainWindow()
{

}

void MainWindow::load_docker_ps()
{
    ui->docker_ps_list->clear();

    auto ps = manager->ps(ui->docker_ps_show_all->isChecked());
    foreach (auto var, ps) {
        QListWidgetItem *item = new QListWidgetItem;
        item->setText(var.names() + ":" + var.status());
        item->setData(990, var.id());
        ui->docker_ps_list->addItem(item);
    }
}

void MainWindow::on_docker_images_list_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    if (!current) return;

    auto id = current->data(990).toString();

    auto images = manager->images();
    foreach (auto var, images) {
        if (id.compare(var.id()) == 0) {
            ui->docker_images_data->setPlainText(var.toJson());
            ui->docker_images_inspect_data->setPlainText(manager->inspect(var.id()).toJson());
        }
    }
}


void MainWindow::on_docker_ps_list_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    if (!current) return;

    auto id = current->data(990).toString();

    auto ps = manager->ps(ui->docker_ps_show_all->isChecked());
    foreach (auto var, ps) {
        if (id.compare(var.id()) == 0) {
            ui->docker_ps_data->setPlainText(var.toJson());
            ui->docker_ps_inspect_data->setPlainText(manager->inspect(var.id()).toJson());
        }
    }
}

void MainWindow::on_docker_search_input_returnPressed()
{
    ui->docker_search_list->clear();

    auto search = manager->search(ui->docker_search_input->text());
    foreach (auto var, search) {
        QListWidgetItem *item = new QListWidgetItem;
        item->setText(var.name() + "(star:" + var.starCount() + ")");
        item->setData(990, var.name());
        ui->docker_search_list->addItem(item);
    }
}

void MainWindow::on_docker_search_list_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    if (!current) return;
    auto name = current->data(990).toString();

    auto search = manager->docker_search_result();
    foreach (auto var, search) {
        if (name.compare(var.name()) == 0)
            ui->docker_search_data->setPlainText(var.toJson());
    }
}

